home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Includes / Geometry.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  8.3 KB  |  271 lines  |  [TEXT/MPS ]

  1. // Geometry.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4. #ifndef __GEOMETRY__
  5. #define __GEOMETRY__
  6.  
  7. // MacApp
  8.  
  9. #ifndef __MACONDITIONALMACROS__
  10. #include "MAConditionalMacros.h"
  11. #endif
  12.  
  13. // for CCharString types
  14. #ifndef __PASCALSTRING__
  15. #include "PascalString.h"
  16. #endif
  17.  
  18. // Toolbox
  19.  
  20. #ifndef __TYPES__
  21. #include <Types.h>
  22. #endif
  23.  
  24. //--------------------------------------------------------------------------------
  25. // Selector operator indicator enumeration. Which coordinate do we want to select? This
  26. // used to be an enumeration, but we do arithmatic on variables of type VHSelect. This is
  27. // not legal C++.
  28. //--------------------------------------------------------------------------------
  29.  
  30. //typedef unsigned char VHSelect;    // use the definition in Types.h
  31. const VHSelect vSel = 0;
  32. const VHSelect hSel = 1;
  33.  
  34.  
  35. //--------------------------------------------------------------------------------
  36. // CPoint: is a toolbox compatible class for the old struct Point. It is bitwise
  37. // compatible with the old struct because it contains no virtual functions.
  38. //--------------------------------------------------------------------------------
  39.  
  40. typedef struct CPoint *CPointPtr;
  41.  
  42. class CPoint : public Point
  43. {
  44. public:
  45.     // Constructors
  46.  
  47.     inline CPoint()                                    // Default constructor.
  48.     { }
  49.  
  50.     inline CPoint(short iH,
  51.           short iV)
  52.     { v = iV; h = iH; }
  53.  
  54.     inline CPoint(const Point& pt)
  55.     { *this = *(const CPoint*)&pt; }
  56.  
  57.     //------------------------------------------------------------------------------------
  58.     // Copy and conversion operator methods
  59.     //------------------------------------------------------------------------------------
  60.     
  61. #if qDebug
  62.     // Conversion operator, for converting CPoint to a textual representation of a
  63.     // CPoint. "CPoint(h,v)".
  64.     inline operator const char*() const
  65.     {
  66.         char textPoint[64];
  67.         sprintf (textPoint, "CPoint(%d, %d)", h, v);
  68.         return CChar63(textPoint);
  69.     }
  70. #endif
  71.     
  72.     // Conversion operators, for converting a CPoint to a long.
  73.     inline operator Point*()
  74.     { return this; }
  75.     
  76.     inline operator const Point*() const
  77.     { return this; }        
  78.  
  79.     inline operator long&()
  80.     { return *((long *) this); }
  81.     
  82.     inline operator const long&() const
  83.     { return *((const long *) this); }
  84.  
  85.     // Selector operators, two are needed one for operating on const Points
  86.     // and the other on non-const Points.
  87.     // Because of inlining, constant sel parameters resolve at compile time.
  88.     
  89.     inline short& operator[](VHSelect sel)
  90.     { return (sel == vSel) ? v : h; }
  91.         // For non-const CPoint
  92.  
  93.     inline const short& operator[](VHSelect sel) const
  94.     { return (sel == vSel) ? v : h; }
  95.         // For const CPoint
  96.  
  97.     // Arithmetic operators
  98.  
  99.     CPoint operator+(const CPoint& pt) const;        // c = a + b    (a,b and c are Points)
  100.     CPoint operator-(const CPoint& pt) const;        // c = a - b    (a,b and c are Points)
  101.  
  102.     CPoint& operator+=(const CPoint& pt);            // c += a        (a and c are points, and
  103.                                                 //                a reference to a is returned,
  104.                                                 //                so these can be strung together)
  105.     CPoint& operator-=(const CPoint& pt);            // c -= a        ( ditto )
  106.  
  107.     // Relational operators
  108.  
  109.     Boolean operator!=(const CPoint& pt) const;    // a != b
  110.     Boolean operator==(const CPoint& pt) const;    // a == b
  111.     Boolean operator>(const CPoint& pt) const;    // a > b
  112.     Boolean operator<(const CPoint& pt) const;    // a < b
  113.     Boolean operator>=(const CPoint& pt) const;    // a >= b
  114.     Boolean operator<=(const CPoint& pt) const;    // a <= b
  115. };
  116.  
  117. //--------------------------------------------------------------------------------
  118. // Selector operator indicator enumeration. Which CPoint do we want
  119. // to select, top-left or bottom-right?
  120. //--------------------------------------------------------------------------------
  121.     
  122. enum PointSelector { topLeft, botRight };
  123.  
  124.  
  125. //--------------------------------------------------------------------------------
  126. // CRect: is a toolbox compatible class for the old struct Rect. It is bitwise
  127. // compatible with the old struct because it contains no virtual functions.
  128. //--------------------------------------------------------------------------------
  129.  
  130. typedef struct CRect *CRectPtr;
  131.  
  132. class CRect : public Rect
  133. {
  134. public:
  135.     // CRect constructors
  136.  
  137.     inline CRect()
  138.     { }
  139.  
  140.     inline CRect(short iLeft, short iTop, short iRight, short iBottom)
  141.     { top = iTop; left = iLeft; bottom = iBottom; right = iRight; }
  142.  
  143.     inline CRect(const CPoint& topLeftPt, const CPoint& botRightPt)
  144.     {
  145.         *(CPoint*)&this->top = topLeftPt;
  146.         *(CPoint*)&this->bottom = botRightPt;
  147.     }
  148.  
  149.     inline CRect(const Rect& rect)
  150.     { *this = *(const CRect*)▭ }
  151.     
  152. #if qPowerPC
  153.     // copy constructor
  154.     inline CRect(const CRect& rect)
  155.     { *(double*)this = *(const double*)▭ }
  156.         // cheat, we know that we're eight bytes long
  157. #endif
  158.  
  159.     //------------------------------------------------------------------------------------
  160.     // Copy and conversion operator methods
  161.     //------------------------------------------------------------------------------------
  162.     
  163. #if qPowerPC
  164.     inline CRect& operator=(const CRect& rect)
  165.     { *(double*)this = *(const double*)▭ return *this; }
  166.         // cheat, we know that we're eight bytes long
  167. #endif
  168.         
  169. #if qDebug
  170.     // Conversion operator, for converting CRect to a textual representation of a
  171.     // CRect. "CRect(left,top,right,bottom)".
  172.     
  173.     inline operator const char*() const
  174.     {
  175.         char textRect[64];    
  176.         sprintf (textRect, "CRect(%d, %d, %d, %d)", left, top, right, bottom);    
  177.         return CChar63(textRect);
  178.     }
  179. #endif
  180.     
  181.     inline operator Rect*()
  182.     { return this; }
  183.     
  184.     inline operator const Rect*() const
  185.     { return this; }
  186.  
  187.     // CPoint selectors for CRect. One for operating on const Rects and one
  188.     // for non-const Rects.
  189.  
  190.     inline CPoint& operator[](PointSelector sel)                // Selector for non-const Rects
  191.     { return (sel == topLeft) ? (*((CPoint *) &top)) : (*((CPoint *) &bottom)); }
  192.  
  193.     inline const CPoint& operator[](PointSelector sel) const    // Selector for const Rects
  194.     { return (sel == topLeft) ? (*((const CPoint *) &top)) : (*((const CPoint *) &bottom)); }
  195.  
  196.     // Operators for adding and subtracting one CRect from to/from another.
  197.  
  198.     CRect operator+(const CRect& rt) const;
  199.     CRect operator-(const CRect& rt) const;
  200.     CRect& operator+=(const CRect& rt);
  201.     CRect& operator-=(const CRect& rt);
  202.  
  203.     // Operators overloaded for adding and subtracting some increment to/from each CPoint
  204.     // along both axis. Very convenient for translating Rects. These take a CPoint and since
  205.     // CPoint has a constructor that takes two shorts the CRect windowRect can be translated
  206.     // 100 pixels in the positive y direction by the statement:
  207.     //
  208.     //    windowRect = windowRect + CPoint (0, 100);    or
  209.     //     windowRect += CPoint (0, 100);
  210.  
  211.     CRect operator+(const CPoint& pt) const;
  212.     CRect operator-(const CPoint& pt) const;
  213.     CRect& operator+=(const CPoint& pt);
  214.     CRect& operator-=(const CPoint& pt);
  215.     
  216.     
  217.     CRect& Inset(const CPoint& delta);
  218.         // Inset a CRect by CPoint.
  219.  
  220.     // Equality operators, other relational operator could be defined such as <. But their
  221.     // meaning is less clear and probably better implemented as methods. For example,
  222.     // aRect < bRect could return true if aRect was inside of bRect, or could return true
  223.     // if the area of aRect was less than the area of bRect.
  224.  
  225.     Boolean operator==(const CRect& rt) const;
  226.     Boolean operator!=(const CRect& rt) const;
  227.  
  228.     // Two simple area operators, the intersection & (bitwise and in C++) and the
  229.     // union | (bitwise or in C++). The definition of union here is to return a CRect
  230.     // that exactly encloses its operands.
  231.  
  232.     CRect operator&(const CRect&) const;
  233.     CRect operator|(const CRect& rt) const;
  234.  
  235.     // Returns true if a valid rectangle (left < right and top < bottom). If not
  236.     // a valid rectangle then return false and set all coordinates to 0.
  237.         
  238.     Boolean Empty() const;
  239.     
  240.     Boolean Valid() const;
  241.         // Returns true if a valid rectangle (left < right and top < bottom).
  242.         
  243.     void Validate();
  244.         // Validate rectagle by switching the points if right-bottom is above or to the
  245.         // left of left-top.
  246.         
  247.     // Returns the length of a CRect in a given dimension.
  248.     
  249.     short GetLength(VHSelect sel) const;
  250.     
  251.     // Returns the size of a CRect in both dimensions. (a CPoint)
  252.     
  253.     CPoint GetSize() const;
  254.     
  255.     // The Contains method takes either a CPoint or a CRect and returns true if the operand
  256.     // is inside of the CRect the method is applied to.
  257.  
  258.     Boolean Contains(const CPoint& pt) const;
  259.     Boolean Contains(const CRect& rt) const;
  260.  
  261. protected:
  262.  
  263.     inline short Min(const short a, const short b) const
  264.     { return a < b ? a : b; }
  265.               
  266.     inline short Max(const short a, const short b) const
  267.     { return a > b ? a : b; }
  268. };
  269.  
  270. #endif
  271.